﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class member_sanyuaninvoice : System.Web.UI.Page
{
	public string EnterpriseName = "";
    public string UserID = "";
    public string UserPower = "";
    public string Footer = "";
	
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Init.islogin(ref UserID,ref EnterpriseName,ref UserPower))
        {
            Response.Redirect("/Login.aspx");
        }        
        if (!UserPower.Contains("Manager"))
        {
            Response.Write("你没有权限！");
            Response.End();
        }
        this.EnableViewState = false;
        Response.Write(Init.InitDefault());
        if (!IsPostBack)
        {
            string xs = Request.Params["xs"];
            dh.Text = xs;
        }
        
        DataSet ds = DataGroup();
        GridView1.DataSource = ds;
        GridView1.DataKeyNames = new string[] { "djbh" };//主键
        GridView1.DataBind();
         
    }
    Common Init = new Common();
    MsSqlHelper mssql = new MsSqlHelper();
    

    //分页
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        DataSet ds = DataGroup();
        GridView1.DataSource = ds;
        GridView1.DataKeyNames = new string[] { "djbh" };//主键
        int recordCount = GridView1.Rows.Count;
        //list(spinfo.Text, pihao.Text);
        GridView1.DataBind();
    }

    //展示
    public DataSet DataGroup(string xs="")
    {

        string djbh =Init.FilteSQLStr(dh.Text);
        string dw = Init.FilteSQLStr(kh.Text);
        string pihao = Init.FilteSQLStr(n.Text);
        string keyword =Init.FilteSQLStr(k.Text);
        string islp = Init.FilteSQLStr(lp.Text);
        string isfp = Init.FilteSQLStr(f.Text);
        string istxd = Init.FilteSQLStr(fs.Text);
        
        string sql = "";
        sql = "select top 1000 rtrim(a.djbh) as djbh,rtrim(b.dwmch) as dwmch,rtrim(a.spid) as spid,rtrim(a.pihao) as pihao,a.islp,rtrim(c.spmch) as spmch,rtrim(c.shpgg) as shpgg,rtrim(c.shengccj) as shengccj,rtrim(a.rq) as rq,'' as sh,'' as fa,'' as ghssh,''ghsfa,'' as rk "
           + " from spls_rk a(nolock), mchk b(nolock),spkfk c(nolock) where a.dwbh=b.dwbh and a.spid=c.spid and a.djbh like 'JHA%' ";

        sql += (!string.IsNullOrEmpty(djbh)) ? " and a.djbh='"+djbh+"'" : "";
        sql += (!string.IsNullOrEmpty(dw)) ? " and (b.danwbh like '%" + dw + "%' or b.dwmch like '%" + dw + "%' or b.zjm like '%" + dw + "%' )" : "";
        sql += (!string.IsNullOrEmpty(pihao)) ? " and a.pihao like '"+pihao+"%' " : "";
        sql += (!string.IsNullOrEmpty(keyword)) ? " and (c.spbh like '%" + keyword + "%' or c.spmch like '%" + keyword + "%' or c.zjm like '%" + keyword + "%' )" : "";

        sql += (islp == "1") ? " and a.islp='是' " : "";
        sql += (islp == "2") ? " and a.islp='否' " : "";


        sql += (isfp == "2") ? " and djbh in(select detailid from [ImageDoc] where type_id='fa' group by detailid) " : "";
        sql += (isfp == "1") ? " and djbh not in(select detailid from [ImageDoc] where type_id='fa' group by detailid) " : "";

        sql += (istxd == "2") ? " and djbh in(select detailid from [ImageDoc] where type_id='sh' group by detailid) " : "";
        sql += (istxd == "1") ? " and djbh not in(select detailid from [ImageDoc] where type_id='sh' group by detailid) " : "";

        sql += "group by a.djbh,a.rq,a.islp,b.dwmch,a.spid,a.pihao,c.spmch,c.shpgg,c.shengccj order by a.djbh desc";
        /*
        if (xs == "")
        {
            sql = "select top 50 rtrim(a.djbh) as djbh,rtrim(b.dwmch) as dwmch,rtrim(a.pihao) as pihao,'' as islp,rtrim(c.spmch) as spmch,rtrim(c.shpgg) as shpgg,rtrim(c.shengccj) as shengccj,rtrim(a.rq) as rq,'' as sh,'' as fa,'' as ghssh,''ghsfa,'' as rk "
            + " from spls_rk a(nolock), mchk b(nolock),spkfk c(nolock) where a.dwbh=b.dwbh and a.spid=c.spid and a.djbh like 'JHA%' group by a.djbh,a.rq,b.dwmch,a.pihao,c.spmch,c.shpgg,c.shengccj order by a.djbh desc";
        }
        else {
            sql = "select top 50 rtrim(a.djbh) as djbh,rtrim(b.dwmch) as dwmch,rtrim(a.pihao) as pihao,'' as islp,rtrim(c.spmch) as spmch,rtrim(c.shpgg) as shpgg,rtrim(c.shengccj) as shengccj,rtrim(a.rq) as rq,'' as sh,'' as fa,'' as ghssh,''ghsfa,'' as rk "
            + " from spls_rk a(nolock), mchk b(nolock),spkfk c(nolock) where a.dwbh=b.dwbh and a.spid=c.spid and a.djbh like 'JHA%' and a.djbh='"+xs+"' group by a.djbh,a.rq,b.dwmch,a.pihao,c.spmch,c.shpgg,c.shengccj order by a.djbh desc";        
        }
         */
        if (Init.IsDebug)
        {
            ResultDeBug.Text = sql;
        }
        DataSet dss = mssql.ExecuteDs(sql);
        for (int i = 0; i < dss.Tables[0].Rows.Count; i++)
        {
            dss.Tables[0].Rows[i]["sh"] = herf(dss.Tables[0].Rows[i]["djbh"].ToString(), dss.Tables[0].Rows[i]["spid"].ToString(), "sh");
            dss.Tables[0].Rows[i]["fa"] = herf(dss.Tables[0].Rows[i]["djbh"].ToString(), dss.Tables[0].Rows[i]["spid"].ToString(), "fa");
            dss.Tables[0].Rows[i]["ghssh"] = herf(dss.Tables[0].Rows[i]["djbh"].ToString(), dss.Tables[0].Rows[i]["spid"].ToString(), "ghssh");
            dss.Tables[0].Rows[i]["ghsfa"] = herf(dss.Tables[0].Rows[i]["djbh"].ToString(), dss.Tables[0].Rows[i]["spid"].ToString(), "ghsfa");
            dss.Tables[0].Rows[i]["rk"] = herf(dss.Tables[0].Rows[i]["djbh"].ToString(), dss.Tables[0].Rows[i]["spid"].ToString(), "rk");
        }
        return dss;
    }

    public string herf(string id,string mxid, string ret)
    {
        DataSet dsmx = new DataSet();
        dsmx.Clear();
        string detailid = id;
        string retsult = "";
        string sql = "select rtrim(imageid) as imageid,rtrim(url) as url from [ImageDoc](nolock) where type_id=rtrim('" + ret + "') and detailid=rtrim('" + detailid + "') and fzxx1='' "
            +" union all "
            + " select rtrim(imageid) as imageid,rtrim(url) as url from [ImageDoc](nolock) where type_id=rtrim('" + ret + "') and detailid=rtrim('" + detailid + "') and fzxx1=rtrim('" + mxid + "') ";
        dsmx = mssql.ExecuteDs(sql);


        if (dsmx.Tables[0].Rows.Count == 0)
        {
            retsult = retsult + " <a class=\"upload\" href=\"javascript:;\" data-type=\"file\" data-dap=\"" + ret + "\" data-id=\"" + id + "\" data-mxid=\"" + mxid + "\" style=\"color: #666; border: 1px solid #d2ccc4;display: inline-block;	margin: 5px 0;\">上传</a> ";
        }
        else
        {

            retsult = retsult + " <a class=\"upload\" href=\"javascript:;\" data-type=\"file\" data-dap=\"" + ret + "\" data-id=\"" + id + "\" data-mxid=\"" + mxid + "\" style=\"color: #ea394c;border: 1px solid #d2ccc4;display: inline-block;	margin: 5px 0;\">上传</a> ";
            int rowid=0;
			for (int i = 0; i < dsmx.Tables[0].Rows.Count; i++)
            {
                string fileurl = Init.imagefileurl(dsmx.Tables[0].Rows[i]["url"].ToString());
                if (!string.IsNullOrEmpty(fileurl))
                {
                    //retsult += " <a class=\"btn-view preview\" href=\"" + fileurl + "\" rel=\"" + dsmx.Tables[0].Rows[i]["rel"].ToString() + "\" " + (rowid == 0 ?"": " style=\"display:none;\"") + ">预览</a>\r\n";
                    retsult = retsult + " <a href=\"" + fileurl + "\"  data-img=\"" + dsmx.Tables[0].Rows[i]["imageid"].ToString() + "\" rel=\"" + id + "\"  src=\"" + fileurl + "\" data-dap=\"" + ret + "\"  class=\"view\" " + (rowid == 0 ? "" : " style=\"display:none;\"") + " style=\"color: #ea394c;border: 1px solid #d2ccc4;display: inline-block;margin: 5px 0;\">预览</a> ";
                    rowid++;
                }
                /*
                if (i == 0)
                {
                    retsult = retsult + " <a href=\"" + dsmx.Tables[0].Rows[i]["url"].ToString() + "\"  data-img=\"" + dsmx.Tables[0].Rows[i]["imageid"].ToString() + "\" rel=\"" + id + "\"  src=\"" + dsmx.Tables[0].Rows[i]["url"].ToString() + "\" data-dap=\"" + ret + "\"  class=\"view\"  style=\"color: #ea394c;border: 1px solid #d2ccc4;display: inline-block;margin: 5px 0;\">预览</a> ";
                }
                else
                {
                    retsult = retsult + " <a href=\"" + dsmx.Tables[0].Rows[i]["url"].ToString() + "\"  data-img=\"" + dsmx.Tables[0].Rows[i]["imageid"].ToString() + "\" rel=\"" + id + "\"  src=\"" + dsmx.Tables[0].Rows[i]["url"].ToString() + "\" data-dap=\"" + ret + "\"  class=\"view\" style=\"display:none;\"  style=\"color: #ea394c;border: 1px solid #d2ccc4;display: inline-block;margin: 5px 0;\">预览</a> ";
                }
                 */
            }
        }
        return retsult;
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        DataSet ds = DataGroup();
        GridView1.DataSource = ds;
        GridView1.DataKeyNames = new string[] { "djbh" };//主键
        int recordCount = ds.Tables[0].Rows.Count;
        GridView1.DataBind();
    }
}